package com.kotlin.zds.activity

import android.Manifest
import android.location.Location
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.amap.api.maps.MapsInitializer
import com.amap.api.maps.model.MyLocationStyle
import com.kotlin.zds.R
import com.kotlin.zds.arouter.Routes
import com.kotlin.zds.base.BaseActivity
import com.kotlin.zds.databinding.ActivityGaodemapBinding
import com.xuexiang.xui.utils.StatusBarUtils
import pub.devrel.easypermissions.EasyPermissions

/**
 * Author: zds
 * Date: 2022/11/5 12:16
 * Description:
 * Version: v1.0
 */
@Route(path = Routes.PATH_GAODE)
class GaodeMapActivity: BaseActivity() ,AMap.OnMyLocationChangeListener{
    var mMapView:MapView?=null
    var aMap: AMap?=null
    val PERMISSIONS_10 = arrayOf(
        Manifest.permission.ACCESS_BACKGROUND_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION)
    val PERMISSIONS_9 = arrayOf(
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION)
    private val REQUEST_CODE_PERMISSION_SMS = 1
    val viewBinding by lazy {
        ActivityGaodemapBinding.inflate(LayoutInflater.from(mActivity))
    }
    override fun onSetContentView() {
        StatusBarUtils.initStatusBarStyle(this,false)
        setContentView(viewBinding.root)
    }


    override fun initView() {
        //地图合规，最先执行
        MapsInitializer.updatePrivacyShow(this,true,true)
        MapsInitializer.updatePrivacyAgree(this,true)
        viewBinding.run {
            mMapView = map
            aMap = mMapView?.map
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mMapView?.onCreate(savedInstanceState)

    }

    override fun initData() {
        var permission: Array<String>? = null
        if (Build.VERSION.SDK_INT>=29){
            permission = PERMISSIONS_10
        }else{
            permission = PERMISSIONS_9
        }
        //动态权限获取
        if (EasyPermissions.hasPermissions(this,*permission)){
            mapLocation()
        }else{
            EasyPermissions.requestPermissions(this, "授予获取定位信息",
                REQUEST_CODE_PERMISSION_SMS, *permission)
        }


    }

    override fun onPause() {
        super.onPause()
        mMapView?.onPause()
    }

    override fun onResume() {
        super.onResume()
        mMapView?.onResume()
    }
    override fun onDestroy() {
        super.onDestroy()
        mMapView?.onDestroy();
    }


    fun mapLocation(){
        var myLocationStyle = MyLocationStyle()
        myLocationStyle.interval(2000)//设置连续定位模式下的定位间隔，只在连续定位模式下生效
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)
        aMap?.myLocationStyle = myLocationStyle //设置定位蓝点的Style
        aMap?.isMyLocationEnabled = true// 设置为true表示启动显示定位蓝点
        aMap?.addOnMyLocationChangeListener(this)
    }

    override fun onMyLocationChange(p0: Location?) {
        p0.run {
            this?.latitude
            this?.longitude
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        super.onPermissionsGranted(requestCode, perms)
        if (requestCode == REQUEST_CODE_PERMISSION_SMS){
            mapLocation()
        }
    }

}